package cn.zzp.sync;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName Producer
 * @Description mq 生产者
 * @Author 祝志鹏
 * @Date 2022/8/16 14:48
 */
public class Producer {


    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {

        // 创建对象
        DefaultMQProducer producer = new DefaultMQProducer("sync_product_group");

        // 设置nameServer地址
        producer.setNamesrvAddr("localhost:9876");

        // 启动
        producer.start();

        // 发送消息
        Message message = new Message("sync", "hello", "我爱我的祖国，一刻也不能分割".getBytes(StandardCharsets.UTF_8));

        SendResult send = producer.send(message);
        System.out.println(send);

        // 关闭
        producer.shutdown();
    }

}
